package com.copc.common.domain;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;
import java.util.List;

/**
 * 分页
 * @author liubo
 */
public class Page<T> extends com.baomidou.mybatisplus.extension.plugins.pagination.Page<T> {
    private static final long serialVersionUID = 8545996863226528798L;

    @ApiModelProperty(value = "数据集" , required = false , example = "[]")
    private List<T> records;
    @ApiModelProperty(value = "总条数" , required = false , example = "0")
    private long total;
    @ApiModelProperty(value = "每一页条数" , required = false , example = "20")
    private long size;
    @ApiModelProperty(value = "当前第几页,从1开始" , required = false , example = "1")
    private long current;

    @JsonIgnore
    @ApiModelProperty(hidden = true)
    private List<OrderItem> orders;
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    private boolean optimizeCountSql = true;
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    protected boolean optimizeJoinOfCountSql = false;
    @JsonIgnore
    @ApiModelProperty(hidden = true)
    private boolean isSearchCount;

    public Page(){}

    public Page(long current, long size) {
        super(current, size, 0L);
    }

    /**
     * 生成一个新的page 对象
     * 但是具数据需要 自己再次填充
     * @param clazz
     * @param <R>
     * @return
     */
    public <R> Page<R> newPage(Class<R> clazz){
        Page<R> newPage = new Page<>(this.getCurrent(),this.getSize());
        newPage.setTotal(this.getTotal());
        return newPage;
    }

    /**
     * 生成一个新的page 对象
     * 具数据为复制后的数据
     * newPage.setRecords(BeanUtil.copyToList(this.getRecords(),clazz));
     * @param clazz
     * @param <R>
     * @return
     */
    public <R> Page<R> newCopyPage(Class<R> clazz){
        Page<R> newPage = new Page<>(this.getCurrent(),this.getSize());
        newPage.setTotal(this.getTotal());
        if (ObjectUtil.isNotEmpty(this.getRecords())){
            newPage.setRecords(BeanUtil.copyToList(this.getRecords(),clazz));
        }
        return newPage;
    }
}
